PyIgnition

https://github.com/animatinator/PyIgnition update for Python 3
Clone: git clone https://git.frombelow.net/PyIgnition.git
Log | Files | Refs | README

Gravity test.py (1492B)


      1 # Gravity test
      2 
      3 import pygame, sys, gravity, math, numpy
      4 
      5 
      6 screen = pygame.display.set_mode((600, 480))
      7 pygame.display.set_caption("ExeSoft particle engine gravity test")
      8 clock = pygame.time.Clock()
      9 
     10 
     11 # Magnitude of a vector
     12 def mag(v):
     13 	return math.sqrt(v[0]**2 + v[1]**2)
     14 
     15 # Draw the supplied gravitational point sources' fields to the supplied surface
     16 def DrawField(sources, surf):
     17 	surf.fill((0, 0, 0))
     18 	maxforce = sources[0].GetMaxForce()  # Not accurate (should take the largest max force) but bollocks to it
     19 	pixels = pygame.surfarray.pixels3d(surf)
     20 	
     21 	for x in range(0, 600):
     22 		for y in range(0, 480):
     23 			forcemag = 0
     24 			for source in sources:
     25 				forcemag += mag(source.GetForce((x, y)))
     26 			
     27 			# Map force to colour (chopped at 255)
     28 			col = (forcemag * 255.0) / 10.0
     29 			if col > 255.0:
     30 				col = 255.0
     31 			
     32 			pixels[x][y] = [col, col, col]
     33 	
     34 	return surf
     35 	
     36 
     37 surf = pygame.Surface((600, 480))
     38 grav = gravity.PointGravity(9.8, (300, 240))
     39 grav2 = gravity.PointGravity(9.8, (400, 300))
     40 surf = DrawField([grav, grav2], surf)
     41 
     42 
     43 while True:
     44 	for event in pygame.event.get():
     45 		if event.type == pygame.QUIT:
     46 			sys.exit()
     47 		elif event.type == pygame.MOUSEMOTION:
     48 			pygame.mouse.get_pos()
     49 	
     50 	screen.blit(surf, (0, 0))
     51 	
     52 	a = pygame.mouse.get_pos()
     53 	f = grav.GetForce(a)
     54 	g = grav2.GetForce(a)
     55 	b = [a[0] + f[0] + g[0], a[1] + f[1] + g[1]]
     56 	pygame.draw.aaline(screen, (0, 255, 0), a, b)
     57 	
     58 	pygame.display.update()
     59 	clock.tick(30)